/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package CircuitTable;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.StringTokenizer;

/**
 *
 * @author Libra
 */
public class Frame extends javax.swing.JFrame {

    /**
     * Creates new form Frame
     */
    public Frame() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        canvas = new CircuitTable.PatternCanvas();
        btnNext = new javax.swing.JButton();
        btnPrev = new javax.swing.JButton();
        txtN = new javax.swing.JTextField();
        btnGoto = new javax.swing.JButton();
        chkFile = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout canvasLayout = new javax.swing.GroupLayout(canvas);
        canvas.setLayout(canvasLayout);
        canvasLayout.setHorizontalGroup(
            canvasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 320, Short.MAX_VALUE)
        );
        canvasLayout.setVerticalGroup(
            canvasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 320, Short.MAX_VALUE)
        );

        btnNext.setText("Next");
        btnNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNextActionPerformed(evt);
            }
        });
        initTriList();

        btnPrev.setText("Prev");
        btnPrev.setEnabled(false);
        btnPrev.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrevActionPerformed(evt);
            }
        });
        initTriList();

        txtN.setText("0");
        txtN.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNActionPerformed(evt);
            }
        });

        btnGoto.setText("Goto");
        btnGoto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGotoActionPerformed(evt);
            }
        });
        initTriList();

        chkFile.setText("Save File");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(134, 134, 134)
                        .addComponent(canvas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(166, 166, 166)
                        .addComponent(btnPrev)
                        .addGap(38, 38, 38)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtN, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnGoto)
                                .addGap(43, 43, 43)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(chkFile)
                                    .addComponent(btnNext))))))
                .addContainerGap(136, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(canvas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(23, 23, 23)
                .addComponent(txtN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnNext)
                    .addComponent(btnPrev)
                    .addComponent(btnGoto))
                .addGap(18, 18, 18)
                .addComponent(chkFile)
                .addContainerGap(134, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNextActionPerformed
        counter++;
        setPara();
        btnPrev.setEnabled(true);
        if (counter == 69) {
            btnNext.setEnabled(false);
        }
    }//GEN-LAST:event_btnNextActionPerformed

    private void btnPrevActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrevActionPerformed
        counter--;
        setPara();
        btnNext.setEnabled(true);
        if (counter == 0) {
            btnPrev.setEnabled(false);
        }
    }//GEN-LAST:event_btnPrevActionPerformed

    private void btnGotoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGotoActionPerformed
        int temp = Integer.parseInt(txtN.getText());
        if (temp >= 0 && temp <= 69) {
            counter = temp;
            setPara();
            if (counter == 0) {
                btnPrev.setEnabled(false);
            } else if (counter == 69) {
                btnNext.setEnabled(false);
            }
        }
    }//GEN-LAST:event_btnGotoActionPerformed

    private void txtNActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtNActionPerformed

    private void setPara() {
        canvas.setConnType(arrTriple[counter]);
        canvas.setPattID(counter);
        canvas.setAct(act[counter]);
        canvas.setSim(sim[counter]);
        canvas.setPLess(pLess[counter]);
        canvas.setPMore(pMore[counter]);
        canvas.refresh();

        txtN.setText(Integer.toString(counter));

        if (chkFile.isSelected()) {
            BufferedImage bi = ScreenImage.createImage(canvas);
            String prefix = counter < 10 ? "0" : "";
            try {
                ScreenImage.writeImage(bi, "image" + prefix + Integer.toString(counter) + ".png");
            } catch (IOException e) {
            }
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Frame().setVisible(true);
            }
        });
    }

    int initTriList() {
        String s = "0,0,0,0,0,0,"
                + "0,0,0,0,0,1,"
                + "0,0,0,0,0,2,"
                + "0,0,0,0,1,1,"
                + "0,0,0,0,1,2,"
                + "0,0,0,0,2,2,"
                + "0,0,0,1,0,1,"
                + "0,0,0,1,0,2,"
                + "0,0,0,1,1,0,"
                + "0,0,0,1,1,1,"
                + "0,0,0,1,1,2,"
                + "0,0,0,2,0,1,"
                + "0,0,0,2,0,2,"
                + "0,0,0,2,2,0,"
                + "0,0,0,2,2,1,"
                + "0,0,0,2,2,2,"
                + "0,0,1,0,0,1,"
                + "0,0,1,0,0,2,"
                + "0,0,1,0,1,1,"
                + "0,0,1,0,1,2,"
                + "0,0,1,0,2,1,"
                + "0,0,1,0,2,2,"
                + "0,0,1,1,0,2,"
                + "0,0,1,1,1,1,"
                + "0,0,1,1,1,2,"
                + "0,0,1,2,0,2,"
                + "0,0,1,2,2,1,"
                + "0,0,1,2,2,2,"
                + "0,0,2,0,0,2,"
                + "0,0,2,0,1,2,"
                + "0,0,2,0,2,2,"
                + "0,0,2,1,1,2,"
                + "0,0,2,2,2,2,"
                + "0,1,0,1,0,1,"
                + "0,1,0,1,0,2,"
                + "0,1,0,1,1,0,"
                + "0,1,0,1,1,1,"
                + "0,1,0,1,1,2,"
                + "0,1,0,2,0,2,"
                + "0,1,0,2,2,0,"
                + "0,1,0,2,2,1,"
                + "0,1,0,2,2,2,"
                + "0,1,1,0,0,2,"
                + "0,1,1,0,1,1,"
                + "0,1,1,0,1,2,"
                + "0,1,1,0,2,2,"
                + "0,1,1,1,0,2,"
                + "0,1,1,1,1,0,"
                + "0,1,1,1,1,1,"
                + "0,1,1,1,1,2,"
                + "0,1,1,2,0,2,"
                + "0,1,1,2,2,0,"
                + "0,1,1,2,2,1,"
                + "0,1,1,2,2,2,"
                + "0,2,0,2,0,2,"
                + "0,2,0,2,2,0,"
                + "0,2,0,2,2,1,"
                + "0,2,0,2,2,2,"
                + "0,2,2,0,1,1,"
                + "0,2,2,0,1,2,"
                + "0,2,2,0,2,2,"
                + "0,2,2,1,1,1,"
                + "0,2,2,1,1,2,"
                + "0,2,2,2,2,0,"
                + "0,2,2,2,2,1,"
                + "0,2,2,2,2,2,"
                + "1,1,1,1,1,1,"
                + "1,1,1,2,2,1,"
                + "1,2,2,2,2,1,"
                + "2,2,2,2,2,2,";
        StringTokenizer st = new StringTokenizer(s, ",");
        arrTriple = new int[st.countTokens() / 6][6];
        for (int i = 0; i < arrTriple.length; i++) {
            for (int j = 0; j < 6; j++) {
                arrTriple[i][j] = Integer.parseInt(st.nextToken());
            }
        }
//        System.out.println(Arrays.deepToString(triList));

        double[] pLessIn = {1,0.0011,0.002,0.7552,1,0.5124,2.00E-04,0,0.4995,0.021,0.9841,0.0264,0.0259,0.1966,0.9999,0.0382,0.7634,4.00E-04,0.1048,0.0846,0.6174,0.2792,0.022,0.6448,0.2066,0.7376,1,0.9056,0.3117,0.2746,0.5541,0.9129,0.7625,0.4701,0.0887,0.4599,0.417,0.5266,0.0755,5.00E-04,0.1023,0.0975,0.0068,0.6504,0.202,0.6108,0.2039,0.8305,0.7176,0.977,0.9175,0.9503,1,0.9328,0.8242,0.0353,0.5797,0.5716,0.1511,0.9092,0.7661,0.8115,1,0.3967,0.9769,0.9241,0.98,1,1,0.9319};
        double[] pMoreIn = {0,0.9994,0.9991,0.3849,2.00E-04,0.6948,1,1,0.648,1,0.0341,0.9894,0.993,0.8644,1.00E-04,0.99,0.3875,1,1,0.9842,0.5774,0.9187,1,1,1,0.484,1.00E-04,0.2162,0.8644,0.9173,1,0.3948,1,1,0.9808,0.7512,1,0.7972,1,1,0.9808,1,1,1,1,1,1,0.5295,1,0.1178,0.2709,0.105,0,0.2435,1,0.9904,0.6946,1,1,0.2258,1,0.436,0,0.8235,0.0782,0.4033,1,0,0,1};
        double[] actIn = {80, 23, 25, 6, 29, 3, 1, 1, 7, 0, 10, 6, 2, 15, 26, 2, 6, 2, 0, 1, 4, 1, 0, 0, 0, 3, 9, 4, 2, 1, 0, 1, 0, 0, 1, 3, 0, 1, 0, 2, 1, 0, 0, 0, 0, 0, 0, 1, 0, 2, 2, 8, 15, 2, 0, 2, 2, 0, 0, 4, 0, 2, 11, 2, 5, 1, 0, 6, 7, 0};
        double[] simIn = {23.8761,39.5637,40.855,5.0096,13.0931,3.5993,9.9879,13.108,7.6763,3.9096,5.1414,13.0772,7.1287,19.0774,12.2309,6.7265,4.9977,13.0888,2.2216,4.0526,4.0599,2.5718,4.0617,0.4339,1.5843,2.5877,1.9023,2.3703,3.5703,2.5757,0.567,0.5005,0.265,0.7468,4.0577,3.873,0.8655,1.5847,2.5686,12.2287,3.8191,2.3891,5.0876,0.425,1.5938,0.4913,1.5707,0.7471,0.3305,0.5955,1.0073,4.7949,1.4768,0.9324,0.1892,6.6335,2.3617,0.5308,1.9032,2.4072,0.2575,1.4805,0.9344,3.0875,2.2565,0.4877,0.0205,0.2881,0.4336,0.0702};
        act = actIn;
        sim = simIn;
        pLess = pLessIn;
        pMore = pMoreIn;
        return arrTriple.length;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnGoto;
    private javax.swing.JButton btnNext;
    private javax.swing.JButton btnPrev;
    private CircuitTable.PatternCanvas canvas;
    private javax.swing.JCheckBox chkFile;
    private javax.swing.JTextField txtN;
    // End of variables declaration//GEN-END:variables
    private int[][] arrTriple;
    private int counter = 0;
    private double[] act, sim, pLess, pMore;
}

